package rad.framework.uml2.classdiagram.edit.parts;

import rad.framework.uml2.classdiagram.edit.policies.AssociationItemSemanticEditPolicy;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Connection;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Property;

/**
 * One2Many
 * 
 * @generated
 */
public class AssociationEditPart extends ConnectionNodeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 3002;

	/**
	 * @generated
	 */
	public AssociationEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new AssociationItemSemanticEditPolicy());
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model so
	 * you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected Connection createConnectionFigure() {
		return new OneToManyFigure();
	}

	/**
	 * @generated
	 */
	public class OneToManyFigure extends
			org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx {

		/**
		 * @generated
		 */
		public OneToManyFigure() {

			setSourceDecoration(createSourceDecoration());
		}

		/**
		 * @NOT generated
		 */
		private org.eclipse.draw2d.PolygonDecoration createSourceDecoration() {
			org.eclipse.draw2d.PolygonDecoration df = new org.eclipse.draw2d.PolygonDecoration();
			// dispatchNext?
			df.setFill(true);
			if (isAggregation()) {
				df.setBackgroundColor(ColorConstants.white);
			}
			org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
			pl.addPoint(-1, 1);
			pl.addPoint(0, 0);
			pl.addPoint(-1, -1);
			pl.addPoint(-2, 0);
			df.setTemplate(pl);
			df.setScale(getMapMode().DPtoLP(9), getMapMode().DPtoLP(5));

			return df;
		}

		private boolean isAggregation() {
			Association association = (Association) ((Edge) getModel())
					.getElement();
			Property property = null;
			if (association.getMemberEnds() != null
					&& association.getMemberEnds().size() > 0) {
				property = (Property) association.getMemberEnds().get(0);
				if (AggregationKind.SHARED_LITERAL.equals(property
						.getAggregation())) {
					return true;
				}
			}
			return false;
		}
	}
}
